package com.qf.controller;

import com.qf.Order;
import com.qf.ResultVo;
import com.qf.dto.OrderDTO;
import com.qf.service.OrderService;
import com.qf.service.WxPayService;
import com.qf.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者：Xx凯丶
 * 时间：2022/4/25 9:15
 */
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/save")
    public ResultVo saveOrder(@RequestBody OrderDTO orderDTO){
        ResultVo resultVO = null;
        Map<String,String> info=new HashMap<>();
        try {
            ResultVo vo = orderService.saveOrder(orderDTO);
            Order order = (Order)vo.getData();

            String orderId = order.getOrderId();
            String untitled = order.getMovieName();
            if (orderId!=null){
                WxPayService wxPayService = new WxPayServiceImpl();
                String payUrl = wxPayService.requestPayUrl(orderId, untitled);
                info.put("payUrl",payUrl);
                info.put("movieName",order.getMovieName());
                info.put("showTime",order.getOrderTime().toString());
                info.put("cinemaName",order.getCinemaName());
                info.put("hallName",order.getHallName());
                info.put("seats",order.getSeats());
                info.put("totalPrice",order.getOrderPrice().toString());
                info.put("orderId",orderId);
                return new ResultVo(200,"SUCCESS",info);
            }else {
                return new ResultVo(400,"FAIL",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ResultVo(400,"FAIL",null);
        }
    }
}
